package org.oep.jmingle;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class JMingle extends MIDlet implements EventPipe {
	public static final String VERSION = "1.1";
	public static final Font DEFAULT_FONT = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL);
	public static final Font BOLD_FONT = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL );
	
	// Our handlers
	private MingleTracker mingleHandler;
	
	// The screens
	private LandingScreen title;
	private Field theField;
	private TutorialScreen theTutorial;
	
	// Misc. crap
	private Display display;
	
	private boolean initialized = false;
	
	
	public static final int SHOW_TUTORIAL = 0;
	public static final int SHOW_FIELD = 1;
	public static final int SHOW_MAIN = 2;
	public static final int QUIT_APP = 3;
	
	public JMingle() {

	}
	
	/*
	 * Inherited from MIDlet
	 */
	//--------------------------------------------------------------//

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		if(mingleHandler.getRecordSize() != mingleHandler.getCacheSize()) {
			mingleHandler.exportToRecordStore();
		}
				
	}

	protected void pauseApp() {
		theField.pause();
		theTutorial.pause();
	}

	protected void startApp() throws MIDletStateChangeException {
		// Please note that the order in which we initialize some things is very
		// important. Things that are constructed using 'this' are in here, and
		// some are interdependent on one another.
		
		if(!initialized) {
			display = Display.getDisplay(this);
			
			// We three screens...
			title = new LandingScreen(this);
			theField = new Field(mingleHandler);
			theTutorial = new TutorialScreen();			
			
			theField.setEventHandler(this);
			theTutorial.setEventPipe(this);
			
			// Initialize some of the major components
			mingleHandler = new MingleTracker(this);
			theField.setTracker(mingleHandler);
			theField.construct();
			
			// Try to show a fluffy loading screen
			LoadScreen loadScreen = new LoadScreen(mingleHandler);
			SplashScreen theSplash = new SplashScreen();
			theSplash.setEventHandler(this);
			display.setCurrent(loadScreen);
			
			// Start the load
			mingleHandler.doLoad();
			loadScreen.die();
			loadScreen = null;
			
			theSplash.goBuddy();
			display.setCurrent(theSplash);
			
			initialized = true;
		}
		
		resumeProperScreen();
	}
	
	/*
	 * Inherited from EventPipe
	 */
	//--------------------------------------------------------------//
	public void eventReceived(int event) {
		switch(event) {
		case SHOW_TUTORIAL: showTutorial(); break;
		case SHOW_FIELD: showField(); break;
		case SHOW_MAIN: showTitle(); break;
		case QUIT_APP: quitApp(); break;
		}
	}
	

	/*
	 * For internal use
	 */
	//--------------------------------------------------------------//
	
	private void showTutorial() {
		theTutorial.toInitialState();
		theTutorial.start();
		display.setCurrent(theTutorial);
	}
	
	private void showField() {
		theField.toInitialState();
		theField.start();
		display.setCurrent(theField);
	}
	
	private void showTitle() {
		title.toInitialState();
		display.setCurrent(title);
	}
	
	private void resumeProperScreen() {
		if(display.getCurrent() == theField)
			theField.start();
		else if(display.getCurrent() == theTutorial) {
			theTutorial.start();
		}
	}
	
	private void quitApp() {
		try {
			destroyApp(false);
		}
		catch(Exception e) { 
			
		}
        notifyDestroyed();	
	}
}
